package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shaoshaohuo.app.R;

import java.util.List;

/**
 * Created by Administrator on 2017/6/10.
 */

public class PayWayAdapter extends BaseAdapter {

    private Context context;
    private List<String> data;
    private List<Integer> commit_pay_picture_list;
    private int selectPosition = -1;

    public PayWayAdapter(Context context, List<String> data, List<Integer> commit_pay_picture_list) {
        this.context = context;
        this.data = data;
        this.commit_pay_picture_list = commit_pay_picture_list;
    }

    public void selectItemState(int selectPosition) {
        this.selectPosition = selectPosition;
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public String getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        PayWayViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.pay_way_item, parent, false);
            holder = new PayWayViewHolder();
            holder.pay_way_item = (TextView) convertView.findViewById(R.id.pay_way_item);
            holder.pay_way_name = (TextView) convertView.findViewById(R.id.pay_way_name);
            holder.pay_way_picture = (ImageView) convertView.findViewById(R.id.pay_way_picture);
            holder.pay_way_select = (ImageView) convertView.findViewById(R.id.pay_way_select);
            convertView.setTag(holder);
        } else {
            holder = (PayWayViewHolder) convertView.getTag();
        }
        Log.e("posiiton", position + "=======");
        if (position == 0) {
            holder.pay_way_select.setVisibility(View.GONE);
            holder.pay_way_name.setVisibility(View.GONE);
            holder.pay_way_picture.setVisibility(View.GONE);
        } else {
            holder.pay_way_item.setVisibility(View.GONE);
            holder.pay_way_select.setVisibility(View.VISIBLE);
            holder.pay_way_name.setVisibility(View.VISIBLE);
            holder.pay_way_picture.setVisibility(View.VISIBLE);
            holder.pay_way_name.setText(data.get(position));
            holder.pay_way_picture.setImageResource(commit_pay_picture_list.get(position - 1));

            if ((position - 1) % 2 == 0) {
                if (selectPosition == position) {
                    convertView.setSelected(true);
                    convertView.setPressed(true);
                    holder.pay_way_select.setImageResource(R.drawable.payselect);
                } else {
                    if (selectPosition == -1) {
                        convertView.setSelected(true);
                        convertView.setPressed(true);
                        holder.pay_way_select.setImageResource(R.drawable.payselect);
                    } else {
                        convertView.setSelected(false);
                        convertView.setPressed(false);
                        holder.pay_way_select.setImageResource(R.drawable.nopayselect);
                    }
                }
            } else {
                if (selectPosition == position) {
                    convertView.setSelected(true);
                    convertView.setPressed(true);
                    holder.pay_way_select.setImageResource(R.drawable.payselect);
                } else {
                    convertView.setSelected(false);
                    convertView.setPressed(false);
                    holder.pay_way_select.setImageResource(R.drawable.nopayselect);
                }
            }


        }


        return convertView;
    }


    class PayWayViewHolder {
        TextView pay_way_item;
        TextView pay_way_name;
        ImageView pay_way_picture;
        ImageView pay_way_select;
    }
}
